package com.ztc.ZtcClasses.service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aqgj.Attachment.bean.Attachment;
import com.aqgj.Attachment.service.AttachmentManager;
import com.ztc.ZtcClasses.bean.ZtcClasses;
import com.ztc.ZtcClasses.dao.ZtcClassesDao;
import com.ztc.ZtcClassesForSale.bean.ZtcClassesForSale;
import com.ztc.ZtcClassesForSale.service.ZtcClassesForSaleManager;
import com.ztc.ZtcClassesPrice.bean.ZtcClassesPrice;
import com.ztc.ZtcClassesPrice.dao.ZtcClassesPriceDao;
import com.ztc.ZtcClassesPriceForSale.bean.ZtcClassesPriceForSale;
import com.ztc.ZtcClassesPriceForSale.service.ZtcClassesPriceForSaleManager;
import com.ztc.ZtcClassesSite.bean.ZtcClassesSite;
import com.ztc.ZtcClassesSite.dao.ZtcClassesSiteDao;
import com.ztc.ZtcClassesSiteForSale.bean.ZtcClassesSiteForSale;
import com.ztc.ZtcClassesSiteForSale.service.ZtcClassesSiteForSaleManager;
import com.ztc.ZtcRoute.bean.ZtcRoute;
import com.ztc.ZtcRouteAttachment.bean.ZtcRouteAttachment;
import com.ztc.ZtcRouteAttachment.dao.ZtcRouteAttachmentDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service("ZtcClasses")
public class ZtcClassesManager {
	@Autowired
	ZtcClassesDao ztcClassesDao;
	@Autowired
	ZtcClassesPriceDao ztcClassesPriceDao;
	@Autowired
	ZtcClassesSiteDao ztcClassesSiteDao;
	@Autowired
	ZtcRouteAttachmentDao ztcRouteAttachmentDao;
	@Autowired
	AttachmentManager attachmentManager;
	@Autowired
	ZtcClassesForSaleManager ztcClassesForSaleManager;
	@Autowired
	ZtcClassesSiteForSaleManager ztcClassesSiteForSaleManager;
	@Autowired
	ZtcClassesPriceForSaleManager  ztcClassesPriceForSaleManager;
	private static final SimpleDateFormat DATE_TIME_FORMET = new SimpleDateFormat("yyyy-MM-dd HH:mm");
	private static final SimpleDateFormat DATE_FORMET = new SimpleDateFormat("yyyy-MM-dd");
	private static final SimpleDateFormat TIME_FORMET = new SimpleDateFormat("HH:mm");
	private static final int autoArangeClassesForSaleDays = 34;
	public String save(ZtcClasses ztcClasses) {
		return ztcClassesDao.save(ztcClasses);
	}

	/**
	 * 获取某班次的所有站点
	 * @param ztcClasses 班次对象
	 * @return 站点集合
	 */
	public List<ZtcClassesSite> getSites(ZtcClasses ztcClasses) {
		ZtcClassesSite site = new ZtcClassesSite();
		site.setFkZtcClassesId(ztcClasses.getId());
		return ztcClassesSiteDao.searchInfoEquals(site);
	}

	/**
	 * 获取某班次的所有价格
	 * @param ztcClasses 班次对象
	 * @return 价格集合
	 */
	public List<ZtcClassesPrice> getPrices(ZtcClasses ztcClasses) {
		ZtcClassesPrice price = new ZtcClassesPrice();
		price.setFkZtcClassesId(ztcClasses.getId());
		return ztcClassesPriceDao.searchInfoEquals(price);
	}

	/**
	 * 获取某班次的所有附件
	 * @param ztcClasses 班次对象
	 * @return 附件集合
	 */
	public List<ZtcRouteAttachment> getAttachments(ZtcClasses ztcClasses) {
		ZtcRoute ztcRoute = new ZtcRoute();
		ztcRoute.setId(ztcClasses.getFkZtcRouteId());
		ZtcRouteAttachment ztcRouteAttachment = new ZtcRouteAttachment();
		ztcRouteAttachment.setFkZtcRouteId(ztcRoute.getId());
		List<ZtcRouteAttachment> ztcRouteAttachments = ztcRouteAttachmentDao.searchInfoEquals(ztcRouteAttachment);
		if (ztcRouteAttachments != null && ztcRouteAttachments.size() > 0) {
			for (int i = 0; i < ztcRouteAttachments.size(); i++) {
				ZtcRouteAttachment routeAttachment = ztcRouteAttachments.get(i);
				if (!"".equals(routeAttachment.getFkAttachmentId()) && routeAttachment.getFkAttachmentId() != null) {
					Attachment attachment = attachmentManager.get(routeAttachment.getFkAttachmentId());
					routeAttachment.setAttachment(attachment);
				}
			}
		}
		return ztcRouteAttachments;
	}

	/**
	 * 根据班次，公司类型，页数组合查询班次
	 * @param ztcClass 班次对象
	 * @param transportType 公司类型
	 * @param pageNumber 第几页
	 * @return 班次集合
	 */
	public List<ZtcClasses> searchInfoLikes(ZtcClasses ztcClass, String transportType, String pageNumber) {
		List<ZtcClasses> list = ztcClassesDao.searchInfoLikes(ztcClass, transportType, pageNumber);
		sortList(list);
		return list;
	}

	public List<ZtcClasses> searchInfoEquals(ZtcClasses ztcClass) {
		List<ZtcClasses> list = ztcClassesDao.searchInfoEquals(ztcClass);
		return list;
	}

	/**
	 * 班次集合排序 ，按两个班次第一个站点的发车时间前后排序
	 * @param list 班次集合
	 */
	private void sortList(List<ZtcClasses> list) {
		Collections.sort(list, new Comparator<ZtcClasses>() {
			@Override
			public int compare(ZtcClasses ztcClassesOne, ZtcClasses ztcClassesTwo) {
				ZtcClassesSite site=new ZtcClassesSite();
				site.setFkZtcClassesId(ztcClassesOne.getId());
				site.setSequence(0);
				ZtcClassesSite siteOne=ztcClassesSiteDao.searchInfoEquals(site).get(0);
				site.setFkZtcClassesId(ztcClassesTwo.getId());
				ZtcClassesSite siteTwo = ztcClassesSiteDao.searchInfoEquals(site).get(0);
				try {
					Date dateOne = DATE_TIME_FORMET
							.parse(ztcClassesOne.getDepartureDate() + " " + siteOne.getDepartureTime());
					Date dateTwo = DATE_TIME_FORMET
							.parse(ztcClassesTwo.getDepartureDate() + " " + siteTwo.getDepartureTime());
					if (dateOne.getTime() < dateTwo.getTime()) {
						return -1;
					} else if (dateOne.getTime() == dateTwo.getTime()) {
						return 0;
					}
				} catch (ParseException e) {
					e.printStackTrace();
				}
				return 1;
			}
		});
	}

	public String delete(ZtcClasses ztcClasses) {
		return ztcClassesDao.delete(ztcClasses);
	}

	public String update(ZtcClasses ztcClasses) {
		return ztcClassesDao.update(ztcClasses);
	}

	/**
	 * 根据名称，公司类型，公司id模糊查找后统计班次数
	 * @param name 名称
	 * @param transportType 公司类型
	 * @param transportId 公司id
	 * @return 统计的班次数
	 */
	public int getCount(String name, String transportType, String transportId) {
		return ztcClassesDao.getCount(name, transportType, transportId);
	}

	/**
	 * 自动排班
	 * @return 排班结果信息：0.失败1.成功
	 * @throws ParseException
	 */
	public String autoArrangeClasses() throws ParseException {
		Date now=new Date();
		List<ZtcClasses> classesList=ztcClassesDao.searchOverdueClasses();
		for(int i=0;i<classesList.size();i++){
			ZtcClasses classesBean = classesList.get(i);
			Date autoArrangeEndDate=classesBean.getAutoArrangeEndDate();
			if(autoArrangeEndDate!=null&&now.getTime()>autoArrangeEndDate.getTime()){
				continue;
			}
			ZtcClassesForSale classesForSale = new ZtcClassesForSale();
			classesForSale.setFkZtcClassesId(classesBean.getId());
			List<ZtcClassesForSale> list = ztcClassesForSaleManager.searchInfoWithDate(classesForSale,
					DATE_FORMET.format(now), null);
			ZtcClassesForSale lastZtcClassesForSale = list.get(list.size() - 1);
			int index = -1;
			List<Date> departureDates=new ArrayList<Date>();
			departureDates = generateDepartureDates(
					DATE_FORMET.format(now),
					String.valueOf(classesBean.getDayFrequenceType()), classesBean.getWeekAllDays(),
					classesBean.getMonthAllDays());
			for (int j= 0; j < departureDates.size()-1; j++) {
				Date departureDate = departureDates.get(j);
				Date departureDateNext=departureDates.get(j+1);
				if (departureDate.getTime()<=lastZtcClassesForSale.getDepartureDate().getTime()&&departureDateNext.getTime()
						>lastZtcClassesForSale.getDepartureDate().getTime()) {
					index = j+1;
					break;
				}else if (departureDateNext.getTime()==lastZtcClassesForSale.getDepartureDate().getTime()) {
					index = j+2;
					break;
				}
			}
			if (index != -1) {
				if (index < departureDates.size()) {
					List<ZtcClassesSite> classesSites = this.getSites(classesBean);
					List<ZtcClassesPrice> classesPrices = this.getPrices(classesBean);
					String data = generateData(classesSites, classesPrices);
					JSONArray dataArray = JSON.parseArray(data);
					JSONArray siteArray = new JSONArray();
					JSONArray priceArray = new JSONArray();
					getSiteAndPriceWithSeatInfoArray(dataArray, siteArray, priceArray, classesBean.getSeatNumber());
					for (int k = index; k < departureDates.size(); k++) {
						Date departureDate = departureDates.get(k);
						if(autoArrangeEndDate!=null&&autoArrangeEndDate.getTime()<departureDate.getTime()){
							continue;
						}
						ZtcClassesForSale ztcClassesForSale = new ZtcClassesForSale();
						ztcClassesForSale.setName(classesBean.getName());
						ztcClassesForSale.setRemark(classesBean.getRemark());
						ztcClassesForSale.setStatus(0);
						ztcClassesForSale.setType(0);
						ztcClassesForSale.setTicketStatus(0);
						ztcClassesForSale.setCarType(classesBean.getCarType());
						ztcClassesForSale.setIsRefund(classesBean.getIsRefund());
						ztcClassesForSale.setDepartureDate(departureDate);
						ztcClassesForSale.setForbidSaleTimeRegionBeforeDeparture(classesBean.getForbidSaleTimeRegionBeforeDeparture());
						ztcClassesForSale.setAddTime(now);
						ztcClassesForSale.setFkAddUserId(classesBean.getFkAddUserId());
						ztcClassesForSale.setFkZtcCarId(classesBean.getFkZtcCarId());
						ztcClassesForSale.setFkZtcDriverId(classesBean.getFkZtcDriverId());
						ztcClassesForSale.setFkZtcClassesId(classesBean.getId());
						ztcClassesForSale.setSeatNumber(classesBean.getSeatNumber());
						ztcClassesForSale.setChildSeatNumber(classesBean.getChildSeatNumber());
						ztcClassesForSale.setFkTransportId(classesBean.getFkTransportId());
						String result = generateClassesForSale(ztcClassesForSale, siteArray, priceArray, classesBean);
						if (result == null) {
							return "0";
						}
					}
				}
			}
		}
		return "1";
	}

	/**
	 * 根据梯形表，座位数生成站点集合和价格集合
	 * @param dataArray 梯形表数据
	 * @param siteArray 站点集合
	 * @param priceArray 价格集合
	 * @param seatNumber 座位数
	 */
	private void getSiteAndPriceWithSeatInfoArray(JSONArray dataArray, JSONArray siteArray, JSONArray priceArray,
												  int seatNumber) {
		siteArray.clear();
		priceArray.clear();
		List<Integer> commonSeatNumbers = new ArrayList<Integer>();
		for(int i=0;i<seatNumber;i++){
			commonSeatNumbers.add(i+1);
		}
		if (dataArray != null) {
			for (int i = 0; i < dataArray.size(); i++) {
				JSONObject siteObject = dataArray.getJSONObject(i);
				String siteId = siteObject.getString("id");
				String siteName = siteObject.getString("name");
				String siteDetail = siteObject.getString("detailed");
				String siteLat = siteObject.getString("lat");
				String managerPhone = siteObject.getString("managerPhone");
				String siteDepartureTime = siteObject.getString("departureTime");
				String daysAfterDepartureDate = siteObject.getString("daysAfterDepartureDate");
				String siteString = "{\"id\":\"" + siteId + "\",\"name\":\"" + siteDetail + "\",\"areaCity\":\""
						+ siteName + "\",\"managerPhone\":\"" + managerPhone + "\",\"lat\":\"" + siteLat
						+ "\",\"departureTime\":\"" + siteDepartureTime + "\",\"daysAfterDepartureDate\":\"" + daysAfterDepartureDate
						+ "\",\"row\":\"" + i + "\"}";
				siteArray.add(JSON.parseObject(siteString));
				JSONArray price = siteObject.getJSONArray("cell");
				if (price != null) {
					for (int j = 0; j < price.size(); j++) {
						JSONObject object = price.getJSONObject(j);
						String priceId = object.getString("id");
						String priceValue = object.getString("pirce");
						String distance = object.getString("distance");
						if (priceValue != null && distance != null && !"".equals(priceValue) && !"".equals(distance)) {
							String priceString = "{\"id\":\"" + priceId + "\",\"price\":\"" + priceValue
									+ "\",\"distance\":\"" + distance + "\",\"seatInfo\":\""
									+ getSeatInfo(commonSeatNumbers, priceValue) + "\",\"row\":\"" + i + "\",\"col\":\""
									+ j + "\"}";
							JSONObject priceObject = JSON.parseObject(priceString);
							priceArray.add(priceObject);
						}
					}
				}
			}
		}
	}

	/**
	 * 根据座位号，价格生成座位信息
	 * @param commonSeatNumbers 座位号集合
	 * @param priceValue 价格
	 * @return 座位信息
	 */
	private String getSeatInfo(List<Integer> commonSeatNumbers, String priceValue) {
		String seatInfo = "";
		for (int i = 0; i < commonSeatNumbers.size(); i++) {
			seatInfo += "(" + commonSeatNumbers.get(i) + ",0," + priceValue + ",0),";
		}
		if (seatInfo.length() > 0) {
			seatInfo = seatInfo.substring(0, seatInfo.length() - 1);
		}
		return seatInfo;
	}

	/**
	 * 根据站点集合，价格集合生成梯形表数据
	 * @param classesSites 站点集合
	 * @param classesPrices 价格集合
	 * @return 梯形表数据
	 */
	private String generateData(List<ZtcClassesSite> classesSites, List<ZtcClassesPrice> classesPrices) {
		String data = "[";
		List<ZtcClassesSite> startZtcClassesSites = new ArrayList<ZtcClassesSite>();
		String startName = "";
		for (int i = 0; i < classesSites.size(); i++) {
			if (i == 0) {
				startName = classesSites.get(i).getName();
			}
			ZtcClassesSite endZctClassesSite = classesSites.get(i);
			String cell = "[";
			for (int j = 0; j < startZtcClassesSites.size(); j++) {
				ZtcClassesSite startZtcClasses = startZtcClassesSites.get(j);
				if (classesPrices.size() == 0) {
					cell += "{\"pirce\":\"\",\"distance\":\"\"}";
					if (j != startZtcClassesSites.size() - 1) {
						cell += ",";
					}
				} else {
					boolean findResult = false;
					int index = 0;
					for (int k = 0; k < classesPrices.size(); k++) {
						ZtcClassesPrice classesPrice = classesPrices.get(k);
						if (startZtcClasses.getId().equals(classesPrice.getFkStartClassesSiteId())
								&& endZctClassesSite.getId().equals(classesPrice.getFkEndClassesSiteId())) {
							index = k;
							cell += "{\"pirce\":\"" + classesPrice.getPrice() + "\",\"startName\":\"" + startName
									+ "\",\"id\":\"" + classesPrice.getId() + "\",\"distance\":\""
									+ classesPrice.getDistance() + "\"}";
							if (j != startZtcClassesSites.size() - 1) {
								cell += ",";
							}
							findResult = true;
							break;
						}
					}
					if (findResult) {
						classesPrices.remove(index);
					} else {
						cell += "{\"pirce\":\"\",\"distance\":\"\"}";
						if (j != startZtcClassesSites.size() - 1) {
							cell += ",";
						}
					}
				}
			}
			cell += "]";
			if (i == 0) {
				data += "{\"name\":\"" + (endZctClassesSite.getAreaCity() == null ? "" : endZctClassesSite.getAreaCity())
						+ "\",\"id\":\"" + endZctClassesSite.getId() + "\",\"managerPhone\":\""
						+ endZctClassesSite.getManagerPhone() + "\",\"lat\":\"" + endZctClassesSite.getLat()
						+ "\",\"row\":\"" + endZctClassesSite.getSequence() + "\",\"daysAfterDepartureDate\":\"" +
						endZctClassesSite.getDaysAfterDepartureDate()
						+ "\",\"departureTime\":\"" + TIME_FORMET.format(endZctClassesSite.getDepartureTime()) + "\",\"remark\":\"起点\",\"detailed\":\""
						+ (endZctClassesSite.getName() == null ? "" : endZctClassesSite.getName()) + "\"}";
			} else if (i == classesSites.size() - 1) {
				data += "{\"name\":\"" + (endZctClassesSite.getAreaCity() == null ? "" : endZctClassesSite.getAreaCity())
						+ "\",\"id\":\"" + endZctClassesSite.getId() + "\",\"managerPhone\":\""
						+ endZctClassesSite.getManagerPhone() + "\",\"lat\":\"" + endZctClassesSite.getLat()
						+ "\",\"row\":\"" + endZctClassesSite.getSequence() + "\",\"daysAfterDepartureDate\":\"" +
						endZctClassesSite.getDaysAfterDepartureDate()
						+ "\",\"departureTime\":\"" + TIME_FORMET.format(endZctClassesSite.getDepartureTime()) + "\",\"remark\":\"终点\",\"detailed\":\""
						+ (endZctClassesSite.getName() == null ? "" : endZctClassesSite.getName()) + "\",\"cell\":" + cell
						+ "}";
			} else {
				data += "{\"name\":\"" + (endZctClassesSite.getAreaCity() == null ? "" : endZctClassesSite.getAreaCity())
						+ "\",\"id\":\"" + endZctClassesSite.getId() + "\",\"managerPhone\":\""
						+ endZctClassesSite.getManagerPhone() + "\",\"lat\":\"" + endZctClassesSite.getLat()
						+ "\",\"row\":\"" + endZctClassesSite.getSequence() + "\",\"daysAfterDepartureDate\":\"" +
						endZctClassesSite.getDaysAfterDepartureDate()
						+ "\",\"departureTime\":\"" + TIME_FORMET.format(endZctClassesSite.getDepartureTime()) + "\",\"detailed\":\""
						+ (endZctClassesSite.getName() == null ? "" : endZctClassesSite.getName()) + "\",\"cell\":" + cell
						+ "}";
			}
			startZtcClassesSites.add(endZctClassesSite);
		}
		data += "]";
		return data;
	}

	/**
	 * 生成在线班次及其站点和价格
	 * @param classesForSale 在线班次对象
	 * @param siteArray 站点集合
	 * @param priceArray 价格集合
	 * @param classesBean   班次对象
	 * @return 操作结果：null.失败，非null.成功
	 * @throws ParseException
	 */
	protected String generateClassesForSale(ZtcClassesForSale classesForSale, JSONArray siteArray, JSONArray priceArray,ZtcClasses classesBean) throws
			ParseException {
		String id = ztcClassesForSaleManager.save(classesForSale);
		if (id != null) {
			// 获取并创建站点对象
			Map<Integer, ZtcClassesSiteForSale> siteMap = new HashMap<>();
			for (int i = 0; i < siteArray.size(); i++) {
				JSONObject object = siteArray.getJSONObject(i);
				if (object == null || object.isEmpty()) {
					continue;
				}
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(classesForSale.getDepartureDate());
				calendar.add(Calendar.DATE, Integer.parseInt(object.getString("daysAfterDepartureDate")));
				ZtcClassesSiteForSale site = new ZtcClassesSiteForSale();
				String siteId = buildClassesSiteForSale(id, object,
						DATE_FORMET.format(calendar.getTime()) + " " + object.getString("departureTime"));
				if (siteId != null && !"".equals(siteId)) {
					site.setId(siteId);
					siteMap.put(object.getInteger("row"), site);
				} else {
					return null;
				}
			}
			for (int i = 0; i < priceArray.size(); i++) {
				JSONObject object = priceArray.getJSONObject(i);
				if (object == null || object.isEmpty()) {
					continue;
				}
				String priceId = buildClassesPriceForSale(id, object, siteMap, classesBean);
				if (priceId == null) {
					return null;
				}
			}
		}
		return id;
	}

	/**
	 * 添加在线班次站点
	 * @param id 在线班次id
	 * @param object 包含在线班次站点信息的对象
	 * @param siteForSaleDepartureTime 站点发车时间
	 * @return 操作结果：null：失败，非null：成功
	 * @throws ParseException
	 */
	private String buildClassesSiteForSale(String id, JSONObject object, String siteForSaleDepartureTime)
			throws ParseException {
		ZtcClassesSiteForSale ztcSiteForSale = new ZtcClassesSiteForSale();
		ztcSiteForSale.setName(object.getString("name"));
		ztcSiteForSale.setManagerPhone(object.getString("managerPhone"));
		ztcSiteForSale.setAreaCity(object.getString("areaCity"));
		ztcSiteForSale.setAreaCode(object.getString("areaCode"));
		ztcSiteForSale.setLat(object.getString("lat"));
		ztcSiteForSale.setStatus(0);
		ztcSiteForSale.setType(0);
		ztcSiteForSale.setSequence(Integer.parseInt(object.getString("row")));
		ztcSiteForSale.setDepartureTime(DATE_TIME_FORMET.parse(siteForSaleDepartureTime));
		ztcSiteForSale.setFkZtcClassesForSaleId(id);
		String siteForSaleId = ztcClassesSiteForSaleManager.save(ztcSiteForSale);
		return siteForSaleId;
	}

	/**
	 *添加在线班次价格
	 * @param id 在线班次id
	 * @param object 包含在线班次价格信息的对象
	 * @param siteForSaleMap 站点集合
	 * @param classesBean 班次实体
	 * @return 操作结果信息：null：失败，非null：成功
	 */
	private String buildClassesPriceForSale(String id, JSONObject object,
											Map<Integer, ZtcClassesSiteForSale> siteForSaleMap, ZtcClasses classesBean) {
		ZtcClassesSiteForSale start = siteForSaleMap.get(object.getInteger("col"));
		if (start == null) {
			return null;
		}
		ZtcClassesSiteForSale end = siteForSaleMap.get(object.getInteger("row"));
		if (end == null) {
			return null;
		}
		ZtcClassesPriceForSale ztcPriceForSale = new ZtcClassesPriceForSale();
		ztcPriceForSale.setFkStartClassesSiteForSaleId(start.getId());
		ztcPriceForSale.setFkEndClassesSiteForSaleId(end.getId());
		ztcPriceForSale.setPrice(object.getString("price"));
		ztcPriceForSale.setDistance(object.getString("distance"));
		ztcPriceForSale.setStatus(0);
		ztcPriceForSale.setSeatInfo(object.getString("seatInfo"));
		ztcPriceForSale.setRemainChildSeatNumber(classesBean.getChildSeatNumber());
		ztcPriceForSale.setRemainSeatNumber(classesBean.getSeatNumber());
		ztcPriceForSale.setFkClassesForSaleId(id);
		String priceForSaleId = ztcClassesPriceForSaleManager.save(ztcPriceForSale);
		return priceForSaleId;
	}

	/**
	 * 按规则生成发车日期集合
	 * @param departureDateString 发车时间
	 * @param dayFrequencyType 按天发班
	 * @param weekAllDays 按周发班
	 * @param monthAllDays 按月发班
	 * @return  发车日期集合
	 * @throws ParseException
	 */
	private List<Date> generateDepartureDates(String departureDateString, String dayFrequencyType, String weekAllDays,
											  String monthAllDays) throws ParseException {
		Date departureDate = DATE_FORMET.parse(departureDateString);
		List<Date> departureDays = new ArrayList<Date>();
		if (!"".equals(dayFrequencyType) && dayFrequencyType != null) {
			departureDays = generateDepartureDateAsDay(departureDate, dayFrequencyType);
		}
		if (!"".equals(weekAllDays) && weekAllDays != null) {
			if (",".equals(weekAllDays)) {
				return null;
			}
			weekAllDays=weekAllDays.substring(1,weekAllDays.length()-1);
			String[] weekDays = weekAllDays.split(",");
			departureDays = generateDepartureDateAsWeek(departureDate, weekDays);
		}
		if (!"".equals(monthAllDays) && monthAllDays != null) {
			if (",".equals(monthAllDays)) {
				return null;
			}
			monthAllDays=monthAllDays.substring(1,monthAllDays.length()-1);
			String[] monthDays = monthAllDays.split(",");
			departureDays = generateDepartureDateAsMonth(departureDate, monthDays);
		}
		return departureDays;
	}

	/**
	 * 规则为按日发班生成发车日期集合
	 * @param departureDate 起始发车日期
	 * @param dayFrequencyType 按日发班规则
	 * @return 发车日期集合
	 */
	private List<Date> generateDepartureDateAsDay(Date departureDate, String dayFrequencyType) {
		List<Date> departureDates = new ArrayList<Date>();
		int intDayFrequencyType =Integer.parseInt(dayFrequencyType);
		for (int i = 0; i <= autoArangeClassesForSaleDays; i = i + intDayFrequencyType) {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(departureDate);
			calendar.add(Calendar.DATE, i);
			departureDates.add(calendar.getTime());
		}
		return departureDates;
	}

	/**
	 * 规则为按周发班生成发车日期集合
	 * @param departureDate 起始发车日期
	 * @param weekDays  按周发车规则
	 * @return 发车日期集合
	 */
	private List<Date> generateDepartureDateAsWeek(Date departureDate, String[] weekDays) {
		List<Date> departureDates = new ArrayList<Date>();
		for (int i = 0; i <= autoArangeClassesForSaleDays; i++) {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(departureDate);
			calendar.add(Calendar.DATE, i);
			for (String day : weekDays) {
				if (!"".equals(day)) {
					day=day.substring(1,day.length()-1);
					if (Integer.parseInt(day) == calendar.get(Calendar.DAY_OF_WEEK) - 1) {
						departureDates.add(calendar.getTime());
					}
				}
			}
		}
		return departureDates;
	}

	/**
	 * 规则为按月发班生成发车日期集合
	 * @param departureDate 起始发车日期
	 * @param monthDays 按月发车规则
	 * @return 发车日期集合
	 */
	private List<Date> generateDepartureDateAsMonth(Date departureDate, String[] monthDays) {
		List<Date> departureDates = new ArrayList<Date>();
		for (int i = 0; i <= autoArangeClassesForSaleDays; i++) {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(departureDate);
			calendar.add(Calendar.DATE, i);
			for (String day : monthDays) {
				if (!"".equals(day)) {
					day=day.substring(1,day.length()-1);
					if (Integer.parseInt(day) == calendar.get(Calendar.DATE)) {
						departureDates.add(calendar.getTime());
					}
				}
			}
		}
		return departureDates;
	}

}
